package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.domain.common.PageResult;
import com.ruoyi.domain.common.ResponseResult;
import com.ruoyi.domain.common.UserHolder;
import com.ruoyi.domain.entity.ForumPost;
import com.ruoyi.domain.entity.ForumPostDto;
import com.ruoyi.param.SearchAndOrder;
import com.ruoyi.service.ForumPostService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 论坛帖子Controller
 * 
 * @author tcl
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/forum")
public class ForumPostController extends ResultBaseController<ForumPostService,ForumPost>
{
    @Autowired
    private ForumPostService forumPostService;

    @GetMapping("/getInfo")
    public ResponseResult<ForumPostDto> getInfo(@RequestParam("id") Integer id){
        return ResponseResult.success(forumPostService.getInfoById(id));
    }

    @PostMapping("/pages")
    public ResponseResult<PageResult<ForumPostDto>> forumPage(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "size",defaultValue = "10") int size, @RequestBody(required = false) SearchAndOrder<ForumPost> searchAndOrder){
        ResponseResult<IPage<ForumPost>> pageResult = super.page(page, size, searchAndOrder);
        ArrayList<ForumPostDto> voList = new ArrayList<>();
        pageResult.getData().getRecords().forEach(item->{
            voList.add(forumPostService.getInfoById(item.getId()));
        });
        int total = Integer.parseInt(String.valueOf(pageResult.getData().getTotal()));
        PageResult<ForumPostDto> forumPostDtoPageResult = new PageResult<>(total, voList);
        return ResponseResult.success(forumPostDtoPageResult);
    }

    @PostMapping("/publish")
    public ResponseResult<String> publish(@RequestBody ForumPost forumPost){
        if(UserHolder.getUserId()==null){
            return ResponseResult.error("请先登录");
        }
        forumPost.setUserId(UserHolder.getUserId());
        forumPost.setCreateTime(new Date());
        if(forumPostService.publishPost(forumPost)){
            return ResponseResult.success("发布成功");
        }
        return ResponseResult.error("发布失败");
    }

    @DeleteMapping("/delete")
    public ResponseResult<String> deletePost(@RequestParam("id") Integer id){
        if(UserHolder.getUserId()==null){
            return ResponseResult.error("请先登录");
        }
        if(UserHolder.getUserId() != forumPostService.getInfoById(id).getUserId()){
            return ResponseResult.error("无权删除权限");
        }
        if(forumPostService.removeById(id)){
            return ResponseResult.success("删除成功");
        }
        return ResponseResult.error("删除失败");
    }

    @PostMapping("/like/{postId}")
    public ResponseResult<String> likePost(@PathVariable("postId") Integer postId){
        if(UserHolder.getUserId()==null){
            return ResponseResult.error("请先登录");
        }
        forumPostService.likePost(postId);
        return ResponseResult.success("点赞已处理");
    }

    @GetMapping("/userIds")
    public ResponseResult<List<Integer>> getLikeUserIds(@RequestParam("forumId") Integer forumId){
        return ResponseResult.success(forumPostService.getLikeUserIds(forumId));
    }

    @GetMapping("/topLikedStockPosts")
    public ResponseResult<List<ForumPostDto>> getStockPosts(@RequestParam("stockId") Integer stockId){
        List<ForumPostDto> topPosts = forumPostService.getTopLikedStockPosts(stockId, 3);
        return ResponseResult.success(topPosts);
    }
}
